/**
 * Humidity sensor module. It cooperates with the capacitance sensor module.
 * It recalculates value of capacitance into value of humidity
 * while providing the same interface Read<double>.
 *
 * @author Paweł Pawłowski <abdul-pablo@tlen.pl>
 * @author Leily Zafari
 **/

#include "humidity.h"

module HumiditySensorM{
	provides {
		interface Read<double>;
	}
	uses {
		interface Read<double> as ReadCapacitance;
	}
}

implementation{

	double humidityValue;
	double sensorValue;
	error_t sensorResult;

	/**
	 * @brief Task processing sensor data and signaling readDone event.
	 *
	 * @return void
	 */
	task void readDoneTask()
	{
		humidityValue = sensorValue / 100;
		signal Read.readDone(sensorResult, humidityValue);
	}

	/**
	 * @brief This event is triggered when the reading process from capacitance sensor is finished.
	 * @details It saves received data and posts readDoneTask()
	 *
	 * @see readDoneTask()
	 *
	 * @param result error_t Status of the finished reading
	 * @param value double Value retrieved from capacitance sensor.
	 * @return void
	 */
	event void ReadCapacitance.readDone(error_t result, double value)
	{
		sensorResult = result;
		sensorValue = value;
		post readDoneTask();
	}

	/**
	 * @brief This command delegates the sensor read call to capacitance sensor.
	 *
	 * @return error_t Status of command invocation.
	 */
	command error_t Read.read(){
		return call ReadCapacitance.read();
	}
}